<?php
class Portfolio extends Controller
{
	private $mod = null;		//model
	function __construct()
	{
		if (!$this->_check_login('user_id'))
		{
			header('Location: /admin/login.html');
			exit();
		}
		
		$this->mod = load_model('portfolio_model');
	}
	
	function getitems()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		
		
		$items = array();
		$items = $this->mod->getItems();
		$count = count($items);
		parent::$tpl->assign('paging_pages', ceil($count / 14));
		parent::$tpl->assign('paging_ipp', 14);		// items per page
		parent::$tpl->assign('items', $items);
		
		if ($inline)
		{
			echo parent::$tpl->fetch('portfolio/list.tpl');
			exit();
		}
	}
	
	public function index()
	{
		$this->getitems();
				
		parent::$tpl->assign('content', parent::$tpl->fetch('portfolio/index.tpl'));
	}
	
	public function warnings()
	{
		
	}
	
	public function add()
	{
		$inline = (isset($_GET['inline']) ? 1: 0);
		
		
		if ($_POST['submit'])
		{
			
			$data = array();
			foreach ($_POST as $key => $value)
				$data[$key] = $value;
			
			if ($data['title'] && $data['body'])
			{
				if ($thumb = $this->handleUploadThumb($_FILES['thumb']))
					$data['thumb'] = $thumb;
				if ($large = $this->handleUploadImage($_FILES['image']))
					$data['large'] = $large;
				
				//add to database
				$this->mod->add($data);
				
				header('Location: /admin/portfolio/');
				exit();
				
			}
			else
				echo 'error';
		}
		
		
		
		
		
		
		parent::$tpl->assign('inline', $inline);
		
		if (!$inline)
		{
			$this->getitems();
			parent::$tpl->assign('content', parent::$tpl->fetch('portfolio/add.tpl'));
		}
		else
		{
			echo parent::$tpl->fetch('portfolio/add.tpl');
			exit();
		}
	}
	
	
	function edit()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		$id = (int) $_GET['id'];
		
		if ($_POST['submit'])
		{
			foreach ($_POST as $key => $value)
				$data[$key] = $value;
			
			
			if ($data['title'] && $data['body'])
			{
				if ($thumb = $this->handleUploadThumb($_FILES['thumb']))
				{
					$this->deleteImage($id, 'thumbs');
					$data['thumb'] = $thumb;
				}
				if ($large = $this->handleUploadImage($_FILES['image']))
				{
					$this->deleteImage($id, 'large');
					$data['large'] = $large;
				}
					
				
				$this->mod->edit($data);
				
				header('Location: /admin/portfolio/');
				exit();
			}
			else
				parent::$tpl->assign('error', 1);
		}
		
		
		
		parent::$tpl->assign('item', $this->mod->getItem($id));
		parent::$tpl->assign('inline', $inline);
		if (!$inline)
		{
			$this->getitems();
			parent::$tpl->assign('content', parent::$tpl->fetch('portfolio/edit.tpl'));
		}
		else
		{
			echo parent::$tpl->fetch('portfolio/edit.tpl');
			exit();
		}
	}
	
	function delete()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		
		
		if ($_POST['submit'])
		{
			$ids = array();
			if ($inline)
			{
				$data = array();
				parse_str($_POST['data'], $data);
				$ids = array_values($data['deleteitem']);
			}
			else
				$ids = array_values($_POST['deleteitem']);
			
			$this->deleteImage($ids, 'thumbs');
			$this->deleteImage($ids, 'large');
			
			$this->mod->delete($ids);
			
			if ($inline)
			{
				$return = array();
				$return['callback'][] = 'updatePortfolioItems()';
				
				echo json_encode($return);
				exit();
				
			}
			else
			{
				header('Location: /admin/portfolio/');
				exit();
			}
		}
	}
	
	
	
	
	
	function getitemjson()
	{
		$res = array();
		
		$res = $this->mod->getItem((int) $_GET['id']);
		
		echo json_encode($res);
		exit();
	}
	
	function getitemsjson()
	{
		$res = array();
		$res = $this->mod->getItems();
		
		echo json_encode($res);
		exit();
	}
	
	
	private function handleUploadThumb($file)
	{
		$filename = $this->formatFileName($file['name']);
		if ($file['name'])
			if (move_uploaded_file ($file['tmp_name'], WEBROOT.'/images/thumbs/'.$filename)) 
				return $filename; 
			else
				return 0;
				
		return 0;
	}
	
	
	private function handleUploadImage($file)
	{
		$filename = $this->formatFileName($file['name']);
		if ($file['name'])
			if (move_uploaded_file ($file['tmp_name'], WEBROOT.'/images/large/'.$filename)) 
				return $filename; 
			else
				return 0;
				
		return 0;
	}
	
	
	private function formatFileName($name)
	{
		$ext = strtolower(substr($name, -3));
		$name = htmlize(strtolower(substr($name, 0, strlen($name)-4))).'.'.time();
		
		return $name.'.'.$ext;
	}
	
	
	private function deleteImage($ids, $path)
	{
		$files = array();
		
		if (is_array($ids))
			$ids = implode(',', $ids);
		else
			$ids = (int) $ids;
			
		$row = ($path == 'thumbs') ? 'thumb' : 'picture';
		$files = $this->mod->getImageFile($ids, $row);
		foreach ($files as $file)
		{
			$path = WEBROOT.'/images/'.$path.'/'.$file;
			@unlink($path);
		}
	}
	
	
	function __destruct(){}
}
?>